package com.example.test.service.impl;

import com.example.test.Dao.CustomersDao;
import com.example.test.Dao.UsersDao;
import com.example.test.bean.Customers;
import com.example.test.bean.DTO.input.CustomersCreateInputDTO;
import com.example.test.bean.DTO.input.CustomersUpdateInputDTO;
import com.example.test.bean.DTO.input.UsersCreateInputDTO;
import com.example.test.bean.DTO.input.UsersUpdateInputDTO;
import com.example.test.bean.DTO.output.CustomersListOutputDTO;
import com.example.test.bean.GoodType;
import com.example.test.bean.Users;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.mapper.CustomersMapper;
import com.example.test.service.CustomersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@Service
public class CustomersServiceImpl extends ServiceImpl<CustomersMapper, Customers> implements CustomersService {


    @Resource
    private CustomersMapper customersMapper;
    private final Logger LOGGER = LoggerFactory.getLogger(CustomersService.class);

    //根据keyword查询
    public R getCustomByKeyword(String keyword, PageModel pageModel){
        try {
            Long current = pageModel.getCurrent();
            Long size = pageModel.getSize();
            Long offset = (current - 1) * size;
            List<CustomersListOutputDTO> CustomerList = customersMapper.getCustomByKeyword(keyword,size,offset);
            if(CustomerList.size()>0){
                return R.success(CustomerList);
            }
            return R.error("未找到对应的账户");
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询用户失败:" + e.getMessage());
        }
    }
    //
    //根据id查询
    public R getCustomById(Long customerId){
        try {
            Customers customer = customersMapper.getCustomById(customerId);
            if(customer != null){
                return R.success(customer);
            }
            return R.error("查询用户失败");
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询用户失败:" + e.getMessage());
        }
    }

    //新增用户
    public R addCustom(CustomersCreateInputDTO customersCreateInputDTO){
        try {
            Date time = new Date();
            Customers newCustomer = new Customers();
            //查询是否有对应的customer，如果没有，则新增
            String customerName = customersCreateInputDTO.getCustomerName();
            if(customerName.length()<1 || customerName == null) {
                return R.error("用户名非法");
            }
            Customers oldCustom = customersMapper.getCustomByName(customerName);
            if(oldCustom == null){
                newCustomer.setDeleted(false);
                newCustomer.setCreateTime(time);
                newCustomer.setCustomerName(customersCreateInputDTO.getCustomerName());
                newCustomer.setPhone(customersCreateInputDTO.getPhone());
                newCustomer.setWechat(customersCreateInputDTO.getWechat());
                newCustomer.setQq(customersCreateInputDTO.getQq());
                newCustomer.setEmail(customersCreateInputDTO.getEmail());
                newCustomer.setAdress(customersCreateInputDTO.getAdress());
                newCustomer.setAmountPurchase(customersCreateInputDTO.getAmountPurchase());
                newCustomer.setFirm(customersCreateInputDTO.getFirm());
                newCustomer.setDeleted(false);
                Integer addCustom = customersMapper.insert(newCustomer);
                if(addCustom == 0){
                    return R.error("新建Customer失败");
                }
                return R.success("插入成功" + newCustomer);
            }return R.error("已有用户！");
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("新增用户失败:" + e.getMessage());
        }
    }

    //更新用户
    public R updateCustom(CustomersUpdateInputDTO customersUpdateInputDTO){
        try {
            Date time = new Date();
            Customers newCustomer = new Customers();
            //查询customerName是否合法
            newCustomer.setDeleted(false);
            newCustomer.setUpdateTime(time);
            newCustomer.setCustomerId(customersUpdateInputDTO.getCustomerId());
            newCustomer.setCustomerName(customersUpdateInputDTO.getCustomerName());
            newCustomer.setPhone(customersUpdateInputDTO.getPhone());
            newCustomer.setEmail(customersUpdateInputDTO.getEmail());
            newCustomer.setQq(customersUpdateInputDTO.getQq());
            newCustomer.setWechat(customersUpdateInputDTO.getWechat());
            newCustomer.setAdress(customersUpdateInputDTO.getAdress());
            newCustomer.setFirm(customersUpdateInputDTO.getFirm());
            newCustomer.setAmountPurchase(customersUpdateInputDTO.getAmountPurchase());
            Integer updateCustom = customersMapper.updateById(newCustomer);
            if(updateCustom == 0){
                return R.error("更新Custom失败");
            }
            return R.success("更新用户成功" + newCustomer);
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("更新用户失败:" + e.getMessage());
        }
    }

    //删除用户
    public R deleteCustom(Long customerId){
        try {
            Date time = new Date();
            Customers customers = new Customers();
            customers.setCustomerId(customerId);
            customers.setDeleted(true);
            customers.setDeletedTime(time);
            Customers oldCustom = customersMapper.getCustomById(customerId);
            if(oldCustom == null){
                R.error("未找到对应用户 删除失败");
            }
            //更新数据库
            int deletenum = customersMapper.updateById(customers);
            if(deletenum == 0){
                return R.error("删除用户失败：deletenum为0");
            }
            return R.success("删除用户成功:"+ customerId);
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("删除用户失败:" + e.getMessage());
        }
    }
}
